package org.datacoder.geocoding.datastructures.intervals

import org.datacoder.geocoding.*
import org.datacoder.geocoding.datastructures.geoshapes.*
import org.datacoder.geocoding.datastructures.points.*
import org.datacoder.geocoding.datastructures.rectangles.*
import org.datacoder.geocoding.jurisdiction.*

class PairedSegmentRangeTrees implements IntersectingIntervalsFinder {

    private SegmentTree segmentTree
    private RangeTree rangeTree
    int type

    public PairedSegmentRangeTrees(Collection<Jurisdiction> jurisdictions, int type){
        this.type = type
        segmentTree = new SegmentTree(jurisdictions, type)
        rangeTree = new RangeTree(jurisdictions, GeoShape.getOtherType(type))
    }

    public Set<Jurisdiction> findIntersectingIntervals(GeoInterval gi){
        Set<Jurisdiction> segmentTreeJurisdictions = segmentTree.findIntervalsContainingPoint(gi.retrieveSingleValue())
        Set<Jurisdiction> rangeTreeJurisdictions = rangeTree.findPointsWithinInterval(gi.retrieveMinDoubleValue(), gi.retrieveMaxDoubleValue())
        segmentTreeJurisdictions.retainAll(rangeTreeJurisdictions)
        return segmentTreeJurisdictions
    }

}

